package cn.cpf.cfets;

import cn.cpf.tool.doc.pdf.StringUtil;
import com.github.cosycode.common.ext.bean.DoubleBean;
import com.github.cosycode.common.lang.ShouldNotHappenException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Arrays;

/**
 * <b>Description : </b>
 * <p>
 * <b>created in </b> 2021/2/1
 *
 * @author CPF
 * @since 1.0
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class FieldRow {

    public static FieldRow geneFieldRow(String[] split) {
        FieldRow fieldRow = new FieldRow();
        int level = 1;
        final int levelCount = split.length - 3;
        fieldRow.setLevelCount(levelCount);

        for (int i = 0; i < levelCount; i++) {
            String item = split[i];
            if ("→".equals(item)) {
                level++;
            } else if (StringUtil.isInteger(item)) {
                fieldRow.setField(Integer.parseInt(item));
                fieldRow.setLevel(level);
            } else if (item.trim().isEmpty()) {
            } else {
                throw new ShouldNotHappenException("这里不应该出现其它情况" + Arrays.toString(split));
            }
        }
        fieldRow.setFieldName(split[levelCount]);
        fieldRow.setTempValue(split[levelCount + 1]);
        fieldRow.setComment(split[levelCount + 2]);
        return fieldRow;
    }

    /**
     * 是否是一行的字符串
     * <p>
     * <b>return String:</b>
     *
     * @param split 行字符串数组
     * @return
     */
    public static DoubleBean<FieldRowMatch, FieldRow> checkAndComposeFieldRow(String[] split) {
        final int len = split.length;
        FieldRowMatch flag = FieldRowMatch.NO_MATCHED;
        int levelCnt = 0;
        String fieldName = "";
        int field = 0;
        String val = "";
        String comment = "";
        int idx = 0;
        for (int i = 0; i < len; i++) {
            String item = split[i];
            switch (flag) {
                case NO_MATCHED:
                    // -> & field
                    if ("→".equals(item)) {
                        levelCnt++;
                    } else if (StringUtil.isInteger(item)) {
                        levelCnt++;
                        idx = levelCnt;
                        field = Integer.parseInt(item);
                        flag = FieldRowMatch.FIELD_MATCHED;
                    } else {
                        return DoubleBean.of(flag, null);
                    }
                    break;
                case FIELD_MATCHED:
                    // -> & field
                    if (item.isEmpty()) {
                        levelCnt++;
                    } else if (isFieldName(item)) {
                        fieldName = item;
                        flag = FieldRowMatch.FIELD_NAME_MATCHED;
                    } else {
                        return DoubleBean.of(flag, null);
                    }
                    break;
                case FIELD_NAME_MATCHED:
                    // vale
                    if (item.isEmpty()) {
                        return DoubleBean.of(flag, null);
                    }
                    flag = FieldRowMatch.VAL_MATCHED;
                    val = item;
                    break;
                case VAL_MATCHED:
                    comment += item;
                    break;
                default:

            }
        }
        if (comment.isEmpty()) {
            return DoubleBean.of(flag, null);
        }
        FieldRow row = new FieldRow();
        row.setLevel(idx);
        row.setField(field);
        row.setFieldName(fieldName);
        row.setLevelCount(levelCnt);
        row.setComment(comment);
        row.setTempValue(val);
        return DoubleBean.of(flag, row);
    }

    public static boolean isFieldName(String fieldName) {
        return !fieldName.isEmpty() && !StringUtil.isInteger(fieldName);
    }

    private int field;
    private String fieldName;
    private int levelCount;
    private int level;
    private String comment;
    private String tempValue;

}
